import pandas as pd
from docx import Document
from docx.shared import Pt, RGBColor, Cm, Inches

from utils.analysis.describe_analysis import describe
from utils.report.base import convert_str_to_float_3, delete_table_first_last_row, set_cell_border, format_table, \
    create_table


def _generate_report(data, document, table_count):
    p1 = (
        f"描述分析用于研究定量数据的整体情况，通过平均值或偏度等信息描述数据的整体情况，从表2-1可以看出：峰度绝对值全部小于3，当前数据分布扁平状态近似正态分布。偏度全部在0附近，当前数据分布偏移状态近似正态分布。"
    )
    paragraph = document.add_paragraph(p1)  # 插入段落
    paragraph.paragraph_format.first_line_indent = Cm(1)
    paragraph.paragraph_format.space_before = Pt(0)  # 段前25磅
    paragraph.paragraph_format.space_after = Pt(0)  # 段后50磅
    _generate_table1(data, document, table_count)  # 表1
    return


def _generate_table1(data, document, table_count):
    # 新建表格
    init_rows = 2
    init_cols = 7
    # table = document.add_table(rows=init_rows, cols=init_cols)
    table = create_table(doc=document, row=init_rows, col=init_cols)
    # 1. 填充1-3行
    # 填充第一行表头
    row = table.rows[0]  # 表格
    f_cell = None
    for i in range(init_cols):  # 合并单元格
        if i == 0:
            f_cell = table.cell(0, 0)
        else:
            f_cell.merge(table.cell(0, i))
    frequency_table_name = ""
    row.cells[0].text = frequency_table_name + '描述统计结果'
    # 填充第二行
    table.cell(1, 0).text = "变量名"
    table.cell(1, 1).text = "最小值"
    table.cell(1, 2).text = "最大值"
    table.cell(1, 3).text = "均值"
    table.cell(1, 4).text = "标准差"
    table.cell(1, 5).text = "偏度"
    table.cell(1, 6).text = "峰度"
    for k, i in data.items():
        table.add_row()  # 表格动态增加一行
        cur_row = table.rows[-1]
        cur_row.cells[0].text = i.name
        cur_row.cells[1].text = convert_str_to_float_3(i.min_value)
        cur_row.cells[2].text = convert_str_to_float_3(i.max_value)
        cur_row.cells[3].text = convert_str_to_float_3(i.avg_value)
        cur_row.cells[4].text = convert_str_to_float_3(i.std_value)
        cur_row.cells[5].text = convert_str_to_float_3(i.skew_value)
        cur_row.cells[6].text = convert_str_to_float_3(i.kurt_value)
    format_table(table)
    add_three_lines_table(table)
    return table


def add_three_lines_table(table):
    delete_table_first_last_row(table)
    first_row = table.rows[1]
    second_row = table.rows[1]
    for cell in first_row.cells:
        set_cell_border(cell, top={"sz": 12, "val": "single", "color": "#000000", "space": "0"})
    for cell in second_row.cells:
        set_cell_border(cell, bottom={"sz": 4, "color": "#000000", "val": "single", "space": "0"})
    last_row = table.rows[-1]
    for cell in last_row.cells:
        set_cell_border(cell, bottom={"sz": 12, "val": "single", "color": "#000000", "space": "0"})
    return


def generate(src_data, doc, table_count):
    title1 = doc.add_heading(level=1)  # 增加标题
    t1_run = title1.add_run('描述统计')
    t1_run.font.color.rgb = RGBColor(10, 10, 10)
    _generate_report(src_data, doc, table_count)  # 生成表格
    return


if __name__ == '__main__':
    # 创建 Document 对象，等价于在电脑上打开一个 Word 文档
    doc = Document()
    pd.set_option('expand_frame_repr', False)

    pd.set_option('expand_frame_repr', False)
    df = pd.read_excel('./test_datas.xlsx')
    src_data = describe(df)
    title1 = doc.add_heading(level=1)  # 增加标题
    t1_run = title1.add_run('描述统计')
    t1_run.font.color.rgb = RGBColor(10, 10, 10)

    # 保存文档
    doc.save('demo.docx')
